package com.dingreading.cloud.soms.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.DescribeException;
import com.dingreading.cloud.common.util.KitUtil;
import com.dingreading.cloud.soms.entity.StoreProjectStage;
import com.dingreading.cloud.soms.entity.StoreProjects;
import com.dingreading.cloud.soms.service.CaClassService;
import com.dingreading.cloud.soms.service.CaScheduleLessonService;
import com.dingreading.cloud.soms.service.StoreProjectStageService;
import com.dingreading.cloud.soms.service.StoreProjectsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/front/storeProjectStage")
@Api(tags = "门店-门店服务阶段管理")
public class FrontStoreProjectStageController {

    @Resource
    protected StoreProjectStageService projectStageService;
    @Resource
    protected StoreProjectsService projectsService;
    @Resource
    protected CaClassService classService;
    @Resource
    protected CaScheduleLessonService scheduleLessonService;


    @ApiOperation(value = "获取服务的阶段")
    @PostMapping(value = "/getList")
    public R<Object> getList(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "服务uid") @RequestParam(value = "projectUid") String projectUid,
            @ApiParam(value = "启用状态") @RequestParam(value = "enabled", required = false) Integer enabled
    ) {
        List<StoreProjectStage> list = projectStageService.getByProjectUid(projectUid, enabled);

        return R.ok(list);
    }

    @ApiOperation(value = "获取启用的服务阶段")
    @PostMapping(value = "/listByEnabled")
    public R<Object> listByEnabled(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "服务uid") @RequestParam(value = "projectUid") String projectUid
    ) {
        List<StoreProjectStage> list = projectStageService.getByProjectUid(projectUid, 1);

        return R.ok(list);
    }

    @Transactional(rollbackFor = DescribeException.class)
    @ApiOperation(value = "新增/修改")
    @PostMapping(value = "/newBean", produces = "application/json; charset=UTF-8")
    public R<Object> newBean(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "uid") @RequestParam(value = "uid", required = false) String uid,
            @ApiParam(value = "服务uid") @RequestParam(value = "projectUid") String projectUid,
            @ApiParam(value = "阶段名称") @RequestParam(value = "stageName") String stageName
    ) throws Exception {
        if (StringUtils.isBlank(stageName))
            return R.fail("阶段名称不能为空");

        StoreProjects projects = projectsService.getByStoreUidAndUid(storeUid, projectUid);
        if (null == projects) return R.fail("服务获取失败");

        boolean b = projectStageService.haveSystemStageName(storeUid, uid, projectUid, stageName);
        if (b) return R.fail("阶段名称不能重复");

        StoreProjectStage data = null;
        if (StringUtils.isBlank(uid)) {
            // 获取最大的序号
            int maxOrderNum = projectStageService.getMaxShowOrder(storeUid, projectUid);
            // 新增
            data = new StoreProjectStage();
            data.setProjectUid(projectUid);
            data.setProjectName(projects.getProjectName());
            data.setOrderNum(maxOrderNum + 1);
            data.setStoreUid(storeUid);
            data.setUid(KitUtil.getUuid());
            data.setSource(2);
            data.setEnabled(1);
            data.setAddStaffUid(staffUid);
            data.setAddStaffName(staffName);
        } else {
            // 修改
            data = projectStageService.getByStoreUidAndUid(storeUid, uid);
            if (data == null) return R.fail("获取失败");
            // 判断能否修改
        }
        data.setStageName(stageName);
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
        projectStageService.saveOrUpdate(data);
        return R.ok();
    }

    @ApiOperation(value = "获取详情")
    @PostMapping(value = "/getBean", produces = "application/json; charset=UTF-8")
    public R<Object> getBean(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "uid") @RequestParam(value = "uid") String uid
    ) {
        StoreProjectStage data = projectStageService.getByStoreUidAndUid(storeUid, uid);
        if (data == null) return R.fail("获取失败");

        return R.ok(data);
    }

    @ApiOperation(value = "更新启用状态")
    @PostMapping(value = "/updateEnabled", produces = "application/json; charset=UTF-8")
    public R<Object> updateEnabled(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "uid") @RequestParam(value = "uid") String uid,
            @ApiParam(value = "状态 0.禁用 1.启用") @RequestParam(value = "enabled") Integer enabled
    ) {
        StoreProjectStage data = projectStageService.getByStoreUidAndUid(storeUid, uid);
        if (data == null) return R.fail("获取失败");
        if (!data.getSource().equals(2)) return R.fail("非门店信息不能操作");

        boolean l = projectStageService.updateEnabled(storeUid, uid, enabled, staffUid, staffName);
        if (!l) return R.fail("更新失败");
        return R.ok();
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/dropBean", produces = "application/json; charset=UTF-8")
    public R<Object> dropBean(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "uid") @RequestParam(value = "uid") String uid
    ) {
        StoreProjectStage data = projectStageService.getByStoreUidAndUid(storeUid, uid);
        if (data == null) return R.fail("获取失败");
        if (!data.getSource().equals(2)) return R.fail("非门店信息不能操作");

        // 先判断
        boolean l = classService.haveStageUid(storeUid, uid);
        if (l)
            return R.fail("社团已使用，不能操作");

        l = scheduleLessonService.haveStageUid(storeUid, uid);
        if (l)
            return R.fail("活动已使用，不能操作");

        long i = projectStageService.dropBean(storeUid, uid);
        if (i <= 0) return R.failDel();
        return R.ok();
    }

    @ApiOperation(value = "上移/下移")
    @PostMapping(value = "/upOrDown", produces = "application/json; charset=UTF-8")
    public R<Object> upOrDown(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "uid") @RequestParam(value = "uid") String uid,
            @ApiParam(value = "类型 1.上移 2.下移") @RequestParam(value = "type") Integer type
    ) {
        StoreProjectStage data = projectStageService.getByStoreUidAndUid(storeUid, uid);
        if (data == null) return R.fail("获取失败");
        if (!data.getSource().equals(2)) return R.fail("非门店信息不能操作");

        if (type.equals(1)) {
            // 上移
            StoreProjectStage preData = projectStageService.getLtOrderNum(storeUid, data.getProjectUid(), data.getOrderNum());
            if (preData == null) return R.fail("已在最前");
            // 和前一条记录的序号互换
            Integer prevOrderNum = preData.getOrderNum();
            Integer curOrderNum = data.getOrderNum();
            // 序号互换
            projectStageService.updateOrderNum(storeUid, preData.getUid(), curOrderNum, staffUid, staffName);
            projectStageService.updateOrderNum(storeUid, uid, prevOrderNum, staffUid, staffName);
        } else if (type.equals(2)) {
            // 下移
            StoreProjectStage backData = projectStageService.getGtOrderNum(storeUid, data.getProjectUid(), data.getOrderNum());
            if (backData == null) return R.fail("已在最后");
            // 和后一条记录的序号互换
            Integer backOrderNum = backData.getOrderNum();
            Integer curOrderNum = data.getOrderNum();
            // 序号互换
            projectStageService.updateOrderNum(storeUid, backData.getUid(), curOrderNum, staffUid, staffName);
            projectStageService.updateOrderNum(storeUid, uid, backOrderNum, staffUid, staffName);
        } else {
            return R.fail("类型错误");
        }
        return R.ok();
    }

}
